﻿@model thailandProject.Models.PAYMENT

@{
    ViewBag.Title = "Edit Client Payment";
}


<div id="Wrapper2">
    <div id="content_header">
        <div id="headerimage">
        </div>
        <div id="theheader">@ViewBag.Header</div>
    </div>
    <!-- Left Column begins here -->
    <aside id="sidebar-wrapper">
        <nav class="sidebar">
            <!-- CONTENT GOES HERE -->
            @using (Html.BeginForm("EditClientPayment", "Clients", FormMethod.Post, new { id = "paymentForm" }))
            {
                string value = Model.paymentIsDeposit ? "true" : "false";
                string payment = @Model.paymentAmount.ToString();
                payment = payment.Substring(0, payment.Length - 2); //Remove the two extra decimal places
                <div id="taskNameArea">
                
                <div id="amountDiv"><span  id="paymentAmountLabel">Amount: <span id="baht">&#3647;</span></span><br /><input type="text" id="paymentAmount" name="paymentAmount" value="@payment" maxlength="10" /><span style="color:red; visibility:hidden;">Invalid Amount</span></div><br />
                <div id="paymentTypeDiv">
                    <span  id="paymentTypeLabel">Type: </span><br />
                    <!--<input type="text" id="paymentType" name="paymentType" /> e.g. Credit Card, Cash, etc.-->
                    <select id="paymentType" name="paymentType">
                        @{
                string paymentType = Model.paymentType ?? "";
                paymentType = paymentType.ToLower();
                int optionNum = 5;
                switch (paymentType)
                {
                    case "pay pal":
                    case "paypal": optionNum = 0; break;
                    case "creditcard":
                    case "credit card": optionNum = 1; break;
                    case "cash": optionNum = 2; break;
                    case "western union": optionNum = 3; break;
                    case "wire transfer": optionNum = 4; break;
                }
                        <option @if (optionNum == 0)
                                { @:selected="selected"
                             }>PayPal</option>
                        <option @if (optionNum == 2)
                                { @:selected="selected"
                            }>Cash</option>
                        <option @if (optionNum == 3)
                                { @:selected="selected"
                            }>Western Union</option>
                        <option @if (optionNum == 5)
                                { @:selected="selected"
                            }>Other</option>
                        }
                    </select>
                </div><br />
                <div id="depositDiv"><span  id="paymentIsDepositLabel">Is Deposit: </span>
                    <span>
                        <input type="hidden" id="precheck" value="@value" />
                        <input id="paymentIsDeposit" name="paymentIsDeposit" type="hidden" value="true" />
                        <img class="hideCompleted_checkbox" src="../../images/checkedBlue.png" id="checkbox" style="width:1.4em;"/>
                        <img src="../../images/unchecked.png" id="image_preload" style="width:1.4em; visibility:hidden;"/>   
                    </span>
                </div><br />
                </div>
                <div id="paymentDetailsArea">
                    <span id="taskDetailsLabel">Notes: </span><br /><textarea id="paymentNotesBox" name="paymentNotes" >@Model.paymentNotes</textarea>
                    <div id="taskDetailsCharsLeft">&nbsp;</div>
                </div>
                
                <input type="hidden" name="paymentID" value="@Model.paymentID" />
                <input type="hidden" name="resID" value="@Model.resID" />
                
                <input type="image" src="../../images/refund_button.png" id="deletePayment" name="btnSubmit" value="Delete" />
                <input type="image" src="../../images/save_button.png" id="savePayment" name="btnSubmit" value="Save" />
                <div id="submitError" style="color:red; visibility:hidden">Please fix errors before continuing.</div>
                <input type="hidden" id="formcheck" value="false" />
            }
            <br />
            @Html.ActionLink("Back", "ViewClientPayments", new { clientId = Model.RESERVATION.clientID })
        </nav>
    </aside>
    <!-- Right column begins here -->
    <article id="contents">
        
    </article>
    <!-- Right column ends here -->
</div>
<!-- Div wrapper2 ends here -->


<script type = "text/javascript">
    $(function () {
        $(".datepicker").datepicker();

        if ($("#precheck").val() == "false") {
            $("#checkbox").attr('src', '../../images/unchecked.png');
        }
    });

    $(".hideCompleted_checkbox").click(function () {
        var indicator = 0;
        var this_id = $(this).attr('id');
        //alert(this_id);
        if ($(this).attr('src') == '../../images/unchecked.png') {
            $(this).attr('src', '../../images/checkedBlue.png');
            $("#paymentIsDeposit").val('true');
            indicator = 1;
        }
        else {
            $(this).attr('src', '../../images/unchecked.png');
            $("#paymentIsDeposit").val('false');
            indicator = 0;
        }
    })

    $("#paymentAmount").keyup(function () {
        //Determine if the price is valid
        var price = $(this).val();
        if (isNaN(price) || price == "" || price.indexOf(' ') != -1 || price < 0) {
            $(this).next().attr('style', 'color:red; visibility:visible;');
        }
        else {
            $(this).next().attr('style', 'color:red; visibility:hidden;');
        }
    })

    $("#paymentNotesBox").keyup(function () {
        //Update the chars left counter
        var message = $(this).val();

        if (message.length > 2000) {
            message = message.substr(0, 2000);
            $(this).val(message);
        }

        $("#taskDetailsCharsLeft").text(2000 - message.length + " chars left");
    })

    $("#paymentForm").submit(function () {
        //Ensure that amount is a valid number
        var button = $("input[type=image][clicked=true]").val();

        if (button == "Save") {
            var price = $("#paymentAmount").val();
            if (isNaN(price) || price == "" || price.indexOf(' ') != -1 || price < 0) {
                $("#submitError").attr('style', 'color:red; visibility:visible;');
                $("#paymentAmount").next().attr('style', 'color:red; visibility:visible;');
                $("#paymentAmount").focus();
                return false;
            }
            else {
                $("#submitError").attr('style', 'color:red; visibility:hidden;');
                $(this).next().attr('style', 'color:red; visibility:hidden;');
                if ($("#formcheck").val() == 'false') {
                    $("#formcheck").val('true');
                    return true;
                }
                else {
                    alert('Your action is currently being processed. Please wait');
                    return false;
                }
            }
        }
        else {
            //Show confirmation dialogue
            if (confirm("Are you sure?\n\nThis payment will be considered refunded.")) {
                return true;
            }
            else {
                $("#delete").attr('clicked', 'false');
                return false;
            }
        }

        
    })

    $("form input[type=image]").click(function () {
        $("input[type=image]", $(this).parents("form")).removeAttr("clicked");
        $(this).attr("clicked", "true");
    });
</script>